using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace ZSW.Framework.Utils
{
    public static class ZSWFUtil_File
    {
        /// <summary>
        /// 递归创建文件夹
        /// </summary>
        public static void CreateDirectoryRecursively(string path)
        {
            if (!Directory.Exists(path))
            {
                CreateDirectoryRecursively(Path.GetDirectoryName(path));
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// 是否存在该文件
        /// </summary>
        public static bool FileExists(string filePath)
        {
            return File.Exists(filePath);
        }

        /// <summary>
        /// 字符串写入文件
        /// </summary>
        public static bool WriteToFile(string content, string filePath, bool overwrite = true)
        {
            bool isExist = FileExists(filePath);
            using (StreamWriter writer = new StreamWriter(filePath, !overwrite))
            {
                writer.Write(content);
            }

            return isExist;
        }

        /// <summary>
        /// 从文件中读取文本
        /// </summary>
        public static string ReadTextFromFile(string filePath)
        {
            try
            {
                return File.ReadAllText(filePath);
            }
            catch (System.Exception e)
            {
                Debug.Log("读取文件内容失败 " + $"Failed to read text from file '{filePath}'. Error: {e.Message}");
                return String.Empty;
            }
        }

        /// <summary>
        /// 从文件中读取文本
        /// </summary>
        public static string[] ReadLinesFromFile(string filePath)
        {
            string[] text;
            try
            {
                text = File.ReadAllLines(filePath);
            }
            catch (System.Exception e)
            {
                Debug.Log("读取文件内容失败 " + $"Failed to read text from file '{filePath}'. Error: {e.Message}");
                return null;
            }

            return text;
        }

        /// <summary>
        /// 读取文件夹下所有的文件
        /// </summary>
        public static string[] GetFilesInFolder(string folderPath, string filter = "")
        {
            List<string> files = new List<string>();

            if (Directory.Exists(folderPath))
            {
                string[] allFiles = Directory.GetFiles(folderPath);

                foreach (string filePath in allFiles)
                {
                    // 筛选掉含有 filter 的文件
                    if (!filePath.Contains(filter))
                    {
                        files.Add(filePath);
                    }
                }
            }
            else
            {
                Debug.LogError($"Directory not found: {folderPath}");
            }

            return files.ToArray();
        }

        /// <summary>
        /// 输出文件名
        /// </summary>
        public static string GetFileName(string filePath, bool includeExtension = true)
        {
            string fileName;

            if (includeExtension)
            {
                // 使用 Path.GetFileName 方法获取文件名（包含后缀名）
                fileName = Path.GetFileName(filePath);
            }
            else
            {
                // 使用 Path.GetFileNameWithoutExtension 方法获取文件名不包含后缀名的部分
                fileName = Path.GetFileNameWithoutExtension(filePath);
            }

            return fileName;
        }

        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="path"></param>
        public static void CreateFile(string path)
        {
            if(!File.Exists(path))
            {
                File.Create(path).Close();
            }
            else
            {
                Debug.Log($"文件已存在。路径：{path}");
            }
        }

    }
}